<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;
//后台登陆
class LoginController extends Controller{
	//登录方法
	public function index(){
		if(IS_POST){
			//验证码是否正确
			$code=Q('post.code','1','strtoupper');
			if($code != $_SESSION['code']){
				View::error("验证码错误");
			}
			//判断用户名是否存在
			$username=Q('post.username');
			$data=Db::table('admin')->where("username='{$username}'")->get();
			if(!$data) View::error("用户名或验证码不正确");
//			p($data);die;
			//判断密码是否正确
			$password=Q('post.password','','md5');
			if($password != $data[0]['password']){
				View::error('用户名或验证码不正确');
			}
			$time=date('Y-m-d H:i:s');
//			p($time);
			Db::table('admin')->where("username='{$username}'")->update(['logintime'=>time()]);
//			$data=Db::table('admin')->where("username='{$username}'")->get();
			
			//登录成功
			$_SESSION['info']=[
				'aid'=>$data[0]['aid'],
				'username'=>$username,
				'logintime'=>$data[0]['logintime']

			];
//			p($_SESSION);die;
			View::success('登录成功',U('Index/index'));
		}
		View::make();
	}
	//显示验证码
	public function code(){
		Code::num(1)->make();
	}
	//退出
	public function out(){
		session_unset();
		session_destroy();
//		go(U('Login/index'));
		echo "<script>window.parent.location.href='".U('Login/index')."'</script>";
		
	}
}


 ?>